﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace Bymed.SpineScan.Repository.Interface
{
    /// <summary>
    /// An instance of IRepository against the specific DataModel type and provide standard CRUD operation only.
    /// </summary>
    /// <typeparam name="T">The specific DataModel type.</typeparam>
    public interface IRepository<T> where T : class
    {
        /// <summary>
        /// Add new entity.
        /// </summary>
        /// <param name="entity">The new entity will be added into database.</param>
        /// <returns>The new added entity.</returns>
        T Add(T entity);

        /// <summary>
        /// Add new entity list.
        /// </summary>
        /// <param name="entities">The new entity list will be added into database.</param>
        /// <returns>The new added entity list.</returns>
        IEnumerable<T> Add(IEnumerable<T> entities);

        // <summary>
        /// Get entity by id. It will return null if the entity does not exist.
        /// </summary>
        /// <param name="id">The specified id.</param>
        /// <returns>The matched data.</returns>
        T Get(object id);

        /// <summary>
        /// Get entity by id. It will never return null. It will always return an entity or throw an exception.
        /// It is free to return a proxy instead. If you know that the value exist in the database, 
        /// and you don’t want to pay the extra select to have that, 
        /// but you want to get that value so we can add that reference to an object.
        /// </summary>
        /// <param name="id">The specified id.</param>
        /// <returns>The matched data.</returns>
        T Load(object id);

        /// <summary>
        /// Query data by lambda expression.
        /// </summary>
        /// <param name="expression">The lambda expression that accepts T and return boolean.</param>
        /// <returns>An IQueryable list of data.</returns>
        IQueryable<T> Query(Expression<Func<T, bool>> expression);

        /// <summary>
        /// Query data by lambda expression.
        /// </summary>
        /// <param name="expression">The lambda expression that accepts T and return boolean.</param>
        /// <param name="includePath">The include path for loading navigation property. 
        /// Multi-Property is allowed. Example: "TestResults;TestResultDiagnosticMessages"</param>
        /// <returns>An IQueryable list of data.</returns>
        IQueryable<T> Query(Expression<Func<T, bool>> expression, string includePath);

        /// <summary>
        /// Get all data. You also can filter the data later since the returned result is a IQueryable collection.
        /// </summary>
        /// <returns>An IQueryable collection of data.</returns>
        IQueryable<T> GetAll();

        /// <summary>
        /// Get all data. You also can filter the data later since the returned result is a IQueryable collection.
        /// </summary>
        /// <param name="expression">The lambda expression that accepts T and return boolean.</param>
        /// <param name="includePath">The include path for loading navigation property. 
        /// Multi-Property is allowed. Example: "TestResults;TestResultDiagnosticMessages"</param>
        /// <returns>An IQueryable list of data.</returns>
        IQueryable<T> GetAll(string includePath);

        /// <summary>
        /// Get the data's count by using provided lambda expression.
        /// </summary>
        /// <param name="expression">The lambda expression that accepts T and return boolean.</param>
        /// <returns>The data's count.</returns>
        int GetRowCount(Expression<Func<T, bool>> expression);

        /// <summary>
        /// Update existing entity.
        /// </summary>
        /// <param name="entity">The entity will be updated.</param>
        void Update(T entity);

        /// <summary>
        /// Update existing entity list.
        /// </summary>
        /// <param name="entities">The entity list will be updated.</param>
        void Update(IEnumerable<T> entities);

        /// <summary>
        /// Add or update entity.
        /// </summary>
        /// <param name="entity">The entity will be updated if it is existing or added if it cannot be found.</param>
        void AddOrUpdate(T entity);

        /// <summary>
        /// Add or update entity list.
        /// </summary>
        /// <param name="entities">The entity list will be updated if it is existing or added if it cannot be found.</param>
        void AddOrUpdate(IEnumerable<T> entities);

        /// <summary>
        /// Delete the specified entity.
        /// </summary>
        /// <param name="entity">The entity will be deleted from database.</param>
        void Delete(T entity);

        /// <summary>
        /// Delete the specified entity list.
        /// </summary>
        /// <param name="entities">The entity list will be deleted from database.</param>
        void Delete(IEnumerable<T> entities);

        /// <summary>
        /// Executes the given DDL/DML command against the database.
        /// 1. Pass raw arguments and use the {0} syntax
        /// 2. Pass DbParameter subclass arguments and use @ParamName syntax.
        /// If you use the first syntax, EF will actually wrap your arguments 
        /// with DbParamater classes, assign them names, and 
        /// replace {0} with the generated parameter name.
        /// </summary>
        /// <param name="sql">The command string.</param>
        /// <param name="parameters">The parameters to apply to the command string.</param>
        void ExecuteSqlCommand(string sql, params Object[] parameters);
    }
}
